/**
 * Created by wxf on 2020/8/31.
 */
//初始化查询栏年份和弹窗内年份日期控件
layui.use('laydate', function(){
    var laydate = layui.laydate;
    //年选择器
    laydate.render({
        elem: '#year'
        ,type: 'year'
        ,value: new Date()
        ,btns: ['now', 'confirm']
    });
    laydate.render({
        elem: '#importYear'
        ,type: 'year'
        ,value: new Date()
        ,btns: ['now', 'confirm']
    });
});
//初始化查询栏公司和弹窗内公司下拉选
layui.use('form', function () {
    var form = layui.form;
    $.ajax({
        url: $ctx+'/company/companyList',
        dataType: 'json',
        type: 'get',
        async: false,
        success: function (data) {
            var res=data.data;
            $.each(res, function (index, item) {
                $('#companies').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                $('#importCompanies').append(new Option(item.name, item.id));// 下拉菜单里添加元素
            });
            form.render("select");
            //重新渲染 固定写法
        }
    });
});
//填充表格数据
layui.use('table', function(){
    var $ = layui.$;
    var table = layui.table;
    var year = $("#year");
    var company = $("#companies");
    var tableRender = table.render({
        elem: '#table'
        ,url:$ctx+'/pfProfit/queryBudget'
        ,where: {year: year.val(), companyId: company.val()}
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field:'id', title: 'ID', hide: true}
            ,{field:'kpiName', title: '科目'}
            ,{field:'01', title: '1月'}
            ,{field:'02', title: '2月'}
            ,{field:'03', title: '3月'} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
            ,{field:'04', title: '4月'}
            ,{field:'05', title: '5月'}
            ,{field:'06', title: '6月'}
            ,{field:'07', title: '7月'}
            ,{field:'08', title: '8月'}
            ,{field:'09', title: '9月'}
            ,{field:'10', title: '10月'}
            ,{field:'11', title: '11月'}
            ,{field:'12', title: '12月'}
        ]]
    });
    //重载
    var active = {
        reload: function(){
            //执行重载
            table.reload('table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    year: $("#year").val(),
                    companyId: $("#companies").val()
                }
            }, 'data');
        }
    };

    $('#queryBtn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    $('#downloadBtn').on('click', function(){
        window.location.href=$ctx+"/pfProfit/exportBudget?year="+year.val()+"&companyId="+company.val();
    });
});
//初始化文件上传
var UPLOAD_FILES={};
layui.use('upload', function(){
    var $ = layui.$,upload = layui.upload;
    //指定允许上传的文件类型
    var layer = layui.layer;
    var uploadRender = upload.render({
        elem: '#selectFile'
        ,url: $ctx+'/pfProfit/importBudgets' //改成您自己的上传接口
        ,accept: 'file' //普通文件
        ,exts: 'xlsx' //只允许上传excel文件
        ,auto:false
        ,bindAction:"#submitFile"
        ,data: {
            year: function(){
                return $('#importYear').val();
            },
            companyId: function(){
                return $('#importCompanies').val();
            }
        }
        ,choose: function(obj){
            //将每次选择的文件追加到文件队列
            UPLOAD_FILES = obj.pushFile();
            clearFile();    //将所有文件先删除再说
            obj.preview(function (index, file, result) {
                obj.pushFile(); //再把当前文件重新加入队列
                $("#fileName").html(file.name);//展示文件名
            });
            uploadRender.config.elem.next()[0].value = '';

        }
        ,before:function () {
            // if(Object.keys(UPLOAD_FILES).length === 0){
            //     layer.msg('请先选择文件！');
            //     return;
            // }
            layer.load(); //上传loading
        }
        ,done: function(res){
            layer.msg('上传成功');
            console.log(res);
            clearFile();
            layer.closeAll();
            $('#queryBtn').click();
        }
        ,error:function (res) {
            layer.error('上传失败');
            layer.closeAll('loading'); //关闭loading
            // clearFile();
        }
    });
    $("#submitFile").on("click",function () {
        if(Object.keys(UPLOAD_FILES).length === 0){
            layer.msg('请先选择文件！');
        }
    });

});
//初始化导入文件弹窗
layui.use('layer', function(){
    var layer = layui.layer,$=layui.$;
    $("#importBtn").on("click",function (){
        layer.open({
            type:1,
            area:['500px','350px'],
            title: '导入预算值',
            content: $("#importForm"),
            shade: 0,
            closeBtn: 0
        });
    });
    $("#cancelImport").on("click",function (){
        clearFile();
        layer.closeAll();
    });
});

//清空文件队列
function clearFile(){
    $("#fileName").html("");
    for (var x in UPLOAD_FILES) {
        delete UPLOAD_FILES[x];
    }
}